//
//  WebVC.swift
//  Auscultation
//
//  Created by 来 on 2021/1/7.
//

import UIKit
import WebKit

class WebVC: BaseVC {
    var completed: (() -> Void)?
    
    var webView: WKWebView!
    var url: String = "" {
        didSet{
            let ss =  url.trimmingCharacters(in: NSCharacterSet.whitespaces)
            let aaa =  ss.trimmingCharacters(in: NSCharacterSet.newlines);
            let str = aaa.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
            
            if let url = URL(string: str!) {
                    let request = URLRequest.init(url: url)
                    self.webView.load(request)
                }
            }
    }
    var titleString = "报告预览"{
        didSet{
            self.navTitle = titleString
            self.lgf_NavigationBar.lgf_Reload()

        }
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        isScreenPanPopEnabled = false
        self.lgf_NavigationBar.lgf_Reload()

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

            let top_view = UIImageView()
            top_view.image = UIImage(named: "table2")
            top_view.isUserInteractionEnabled = true
            self.view.addSubview(top_view)
            top_view.snp.makeConstraints { (make) in
                make.top.left.right.equalToSuperview()
                make.height.equalTo(UIDevice.lgf_NavBarH())
            }
            let style = LGFNavigationBarStyle.lgf()
            style.lgf_LeftBtnImageLight = UIImage(named: "darkback")!
            self.lgf_NavigationBar.lgf_line?.isHidden = true

            self.lgf_NavigationBar.lgf_Reload()
            
            self.lgf_ShowWebProgress(UIDevice.lgf_NavBarH(), 3, "2C75EC")
            self.webView = WKWebView.init()
            self.webView.uiDelegate = self
            self.webView.navigationDelegate = self
            self.view.addSubview(self.webView)
            self.webView.snp.makeConstraints { (make) in
                make.top.equalTo(top_view.snp.bottom)
                make.left.right.bottom.equalToSuperview()
            }
    }
    
    @objc override func leftBtnSelect() {
        completed?()
        self.navigationController?.popToRootViewController(animated: true)
    }



}


extension WebVC: WKNavigationDelegate, WKUIDelegate {
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { }
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        lgf_HideWebProgress()
        webView.becomeFirstResponder()
    }
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        lgf_HideWebProgress()
    }
    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) { }
    
    func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        if challenge.protectionSpace.authenticationMethod.elementsEqual(NSURLAuthenticationMethodServerTrust) {
            let card = URLCredential.init(trust: challenge.protectionSpace.serverTrust!)
            let disposition = URLSession.AuthChallengeDisposition.useCredential
            completionHandler(disposition ,card)
        }
    }
}
